<template>
  <div>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="30%" :append-to-body="true" @close="handleClose">
      <el-form label-width="80px" :model="formDate" :rules="rules" ref="ruleForm">
        <el-form-item label="跟进方式:" prop="interactId">
           <el-select v-model="formDate.trackingMethod" clearable filterable placeholder="请选择">
              <el-option
                v-for="item in interactList"
                :key="item.trackingMethod"
                size="small"
                :label="item.methodName"
                :value="item.trackingMethod">
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="沟通方式:" prop="interactType">
          <el-input type="text" placeholder="请输入沟通方式" v-model="formDate.interactType" size="small" clearable
            style="width: 60%">
          </el-input>
        </el-form-item>
        <el-form-item label="排序:">
          <el-input-number v-model="formDate.sortOrder" :min="0" label="请排序">
          </el-input-number>
        </el-form-item>
        <el-form-item label="状态:">
          <el-radio-group v-model="formDate.isEnable">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="btnOk">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getTrackingMethodListAPI, addInteractEditAPI, getInteractDetailAPI } from "@/api/fms.js";
const defaultListQuery = {
  trackingMethod: "", //跟进方式id
  interactType: "", //沟通方式名称
  sortOrder: 0,//排序值
  isEnable: 0,//是否启用
};
export default {
  name: "addCommunication",
  data() {
    return {
      dialogVisible: false,
      formDate: JSON.parse(JSON.stringify(defaultListQuery)),
      interactList: [],
      title:"",
      rules: {
        trackingMethod: [
          { required: true, message: '请选择跟进方式', trigger: 'blur' },
        ],
        interactType: [
          { required: true, message: '请输入沟通方式', trigger: 'blur' },
        ],
      }
    };
  },
  created() {
    getTrackingMethodListAPI({
      pageNum: 1,
      pageSize: 100,
    }).then((res) => {
      res.list.forEach((item) => {
        this.interactList.push({
          methodName: item.trackingMethod,
          trackingMethod: item.tmId
        })
      })
    }).catch(() => { })
  },
  methods: {
    // 提交
    async btnOk() {
      let res = null
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (this.formDate.tmId) {
            res = await addInteractEditAPI({
              ...this.formDate,
              interactId: this.formDate.tmId
            });
          } else {
            res = await addInteractEditAPI(this.formDate);
          }
          if (res) {
            this.$message({
              type: "success",
              message: "操作成功!",
            });
          }
          this.handleClose();
          this.$emit("upload");
        } else {
          return false;
        }
      })
    },
    // 回显
    async showDetail(tmId) {
      this.dialogVisible = true;
      this.title = tmId ? "修改" : "新增"
      if (tmId) {
        const res = await getInteractDetailAPI({
          interactId: tmId
        })
        this.formDate = { ...res }
      }
    },
    // 关闭弹框
    handleClose() {
      this.formDate = JSON.parse(JSON.stringify(defaultListQuery))
      this.$refs.ruleForm.resetFields();
      this.dialogVisible = false;
    },
  },
};
</script>
<style lang="scss">
.avatar-uploader-icon {
  margin-bottom: 10px;
  display: inline-block;
  width: 80px;
  height: 80px;
  border: 1px solid #dcdfe6;
  text-align: center;
  line-height: 80px;
  vertical-align: middle;
}
</style>
